<?php
//Include the code
require_once 'phplot.php';
//Define the object
$plot = new PHPlot(1024,768);
$date = $_GET["date"];
$sensor = $_GET["sensor"];
//ket noi toi database
$conn=mysql_connect("localhost","root","abc123") or die("Not connect");
mysql_select_db("detaiptntd",$conn);
//cau lenh truy van		

if($sensor=="")
{
	$sql1 = "select distinct time from enviam where date = '".$date."'";
	//thuc thi truy van
	$query1=mysql_query($sql1);
	$numrow1 = mysql_num_rows($query1);	
	if($numrow1>0)
	{
		while($row1=mysql_fetch_array($query1))
		{
			$sql2 = "select value,idSS from enviam where date = '".$date."' && time='".$row1[0]."' order by idSS";
			$query2=mysql_query($sql2);
			$numrow2 = mysql_num_rows($query2);
			//mang chua thong tin gia tri cac sensor tai cung mot thoi diem time = row1[0]
			$dt = array();
			//dua thoi gian la phan tu dau tien cua mang
			array_push($dt,$row1[0]);
			while($row2=mysql_fetch_array($query2))
			{
				//cac phan tu tiep theo cua mang la cac gia tri cua cac sensor tai thoi diem row1[0]
				array_push($dt,$row2[0]);								
			}
			
			//gan vao mang 2 chieu gia tri cua cac sensor o tung thoi diem
			$data[] = $dt;
			
		}
	}
	$sql3 = "Select name from sensor where idSS in (SELECT distinct idSS from enviam where  date = '".$date."')";
	$query3=mysql_query($sql3);
	//mang chua ten cac sensor
	$dtname = array();
	while($row3=mysql_fetch_array($query3))
	{
		array_push($dtname,$row3[0]);
	}		
				
	//gan data cho bieu do
	$plot->SetDataValues($data);
	//gan legend
	$plot->SetLegend($dtname);
	//dat vi tri legend
	$plot->SetLegendPosition(-4.5, 0, 'title', 0.5, 1);
	$plot->SetLegendUseShapes(TRUE);
	//gan title cho cac gia tri
	$plot->SetTitle("Graph Sensor");
	$plot->SetXTitle('Time');
	$plot->SetYTitle('Value');
	//Turn off X axis ticks and labels because they get in the way:
	$plot->SetXTickLabelPos('none');
	$plot->SetXTickPos('none');
	//Ve bieu do
	$plot->DrawGraph();
}
else
{
	$sql = "select time,value from enviam where idSS = '".$sensor."' && date='".$date."'";
	//thuc thi truy van
	$query=mysql_query($sql);
	$numrow = mysql_num_rows($query);
	$data = array();
	for ($i = 0; $i < $numrow; $i++) $data[] = mysql_fetch_row( $query );
	$plot->SetDataValues($data);
	//gan title cho cac gia tri
	$plot->SetTitle("Graph Sensor");
	$plot->SetXTitle('Time');
	$plot->SetYTitle('Value');
	//Turn off X axis ticks and labels because they get in the way:
	$plot->SetXTickLabelPos('none');
	$plot->SetXTickPos('none');
	//Ve bieu do
	$plot->DrawGraph();
}	

?>